<?php

/**
* alltosun.com 快递管理 index.php for newbd
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* Author: 吴松键 (wusj@alltosun.com)
* Date: 15/2/4 下午12:37
*/

class Action
{
    private $per_page = 20;

    /**
     * 快递列表
     */
    public function index()
    {
        $page          = Request::Get('page_no', 1);
        $status        = Request::Get('status', 1);
        $search_filter = Request::getParam('search_filter', array());

        // 拼装搜索条件
        $is_empty = 0;
        $filter = array();
        if (isset($search_filter['id']) && $search_filter['id']) {
            $filter['id'] = $search_filter['id'];
        }
        if (isset($search_filter['status']) && $search_filter['status']) {
            $filter['status'] = $search_filter['status'];
        }
        if (isset($search_filter['title']) && $search_filter['title']) {
            $filter['`title` LIKE'] = '%'.$search_filter['title'].'%';
        }
        $filter['status'] = $status;

        $express_list = array();
        if (!$is_empty) {
            $count = _model('express')->getTotal($filter);
            if ($count) {
                $pager = new Pager($this->per_page);
                if ($pager->generate($count)) {
                    Response::assign('pager', $pager);
                }

                $express_list = _model('express')->getList($filter, ' ORDER BY `add_time` DESC '.$pager->getLimit($page));
            }
        }

        Response::assign('express_list', $express_list);
        Response::assign('status', $status);
        Response::assign('search_filter', $search_filter);
        Response::display('admin/express_list.html');
    }

    /**
     * 快递区域列表
     */
    public function area()
    {
        $express_id = Request::getParam('express_id', 0);
        $status     = Request::getParam('status', 1);

        if (!$express_id) {
            return array('请选择需要管理的快递公司', 'error', AnUrl('express/admin'));
        }

        $filter = array(
            'status' => $status,
            'express_id' => $express_id
        );
        $list = _model('express_relation')->getList($filter);

        Response::assign('list', $list);
        Response::assign('express_id', $express_id);
        Response::assign('status', $status);

        Response::display('admin/area_list.html');
    }

    /**
     * 添加快递
     */
    public function add()
    {
        $express_id = Request::getParam('express_id', 0);

        if ($express_id) {
            $express_info = _uri('express', $express_id);

            Response::assign('express_info', $express_info);
        }

        Response::display('admin/express_add.html');
    }

    /**
     * 添加区域
     */
    public function add_area()
    {
        $express_relation_id = Request::getParam('express_relation_id', 0);
        $express_id          = Request::getParam('express_id', 0);

        if ($express_relation_id) {
            $express_relation_info = _uri('express_relation', $express_relation_id);
            $provincial_list = express_helper::get_provincial_list($express_relation_id);
            $express_id = $express_relation_info['express_id'];

            Response::assign('provincial_list', $provincial_list);
            Response::assign('express_relation_info', $express_relation_info);
        }

        if (!$express_id) {
            return array('请选择需要操作的快递', 'error', AnUrl('express/admin'));
        }

        Response::assign('express_id', $express_id);

        Response::display('admin/area_add.html');
    }

    /**
     * 保存快递信息
     */
    public function save()
    {
        $express_id = Request::getParam('express_id', 0);
        $info = Request::getParam('info', array());

        foreach ($info as $k=>$v) {
            if (!is_numeric($v)) {
                $info[$k] = trim(AnFilter::filter_string($v));
            }
        }

        if ($express_id) {
            _model('express')->update(array('id'=>$express_id), $info);
        } else {
            _model('express')->create($info);
        }

        Response::redirect(AnUrl('express/admin/index'));
    }

    /**
     * 保存区域信息
     */
    public function save_area()
    {
        $express_relation_id = Request::getParam('express_relation_id', 0);
        $express_id          = Request::getParam('express_id', 0);
        if (!$express_id) {
            return array('请选择需要操作的快递', 'error', AnUrl('express/admin'));
        }

        $info = Request::getParam('info', array());
        $provincial_ids = Request::getParam('provincial_ids', '');

        foreach ($info as $k=>$v) {
            if (!is_numeric($v)) {
                $info[$k] = trim(AnFilter::filter_string($v));
            }
        }

        $info['express_id'] = $express_id;
        if ($express_relation_id) {
            _model('express_relation')->update(array('id'=>$express_relation_id), $info);
        } else {
            $express_relation_id = _model('express_relation')->create($info);
        }

        //更新区域列表
        $provincial_ids = explode(',', $provincial_ids);

        // 删除当前状态不存在的关联
        $filter = array(
            'express_id' => $express_id,
            'express_relation_id' =>$express_relation_id,
            '`provincial_id` NOT ' => $provincial_ids,
        );
        _model('provincial_relation')->delete($filter);

        if ($provincial_ids) {
            // 添加新选项
            unset($filter['`provincial_id` NOT ']);
            $old_provincial_ids = _model('provincial_relation')->getFields('provincial_id', $filter);
            foreach ($provincial_ids as $k=>$v) {
                if (0 == $v || !$v) {
                    continue;
                }
                if (!in_array($v, $old_provincial_ids)) {
                    $info = array(
                        'express_id' => $express_id,
                        'express_relation_id' =>$express_relation_id,
                        'provincial_id' => $v
                    );

                    _model('provincial_relation')->create($info);
                }
            }
        }

        Response::redirect(AnUrl('express/admin/area?express_id='.$express_id));
    }

    /**
     * 删除
     * @return array
     */
    public function delete()
    {
        $id = Request::getParam('express_id');

        if (!$id) {
            return '请选择要删除的内容';
        }

        $ids = explode(',', trim($id, ','));
        foreach ($ids as $k=>$v) {
            if (!$v) {
                continue;
            }

            $focus_info = _uri('express', (int)$v);
            if (!$focus_info) {
                continue;
            }

            if (1 == $focus_info['status']) {
                _model('express')->update($v, array('status'=>0));
            }
        }

        return 'ok';
    }

    /**
     * 更新状态
     * @return array
     */
    public function change_status()
    {
        $id     = Request::getParam('express_id', 0);
        $status = Request::getParam('status', 1);
        if (!$id) {
            return '请选择要更改的内容';
        }

        $focus_info = _uri('express', $id);
        if ($status == $focus_info['status']) {
            return '已经执行过此操作，不用重复提交';
        }

        if (1 == $status) {
            _model('express')->update($id, array('status'=>1));
        }

        return 'ok';
    }

    /**
     * 删除
     * @return array
     */
    public function delete_area()
    {
        $id = Request::getParam('express_relation_id');

        if (!$id) {
            return '请选择要删除的内容';
        }

        $ids = explode(',', trim($id, ','));
        foreach ($ids as $k=>$v) {
            if (!$v) {
                continue;
            }

            $focus_info = _uri('express_relation', (int)$v);
            if (!$focus_info) {
                continue;
            }

            if (1 == $focus_info['status']) {
                _model('express_relation')->update($v, array('status'=>0));
            }
        }

        return 'ok';
    }

    /**
     * 更新状态
     * @return array
     */
    public function change_status_area()
    {
        $id     = Request::getParam('express_relation_id', 0);
        $status = Request::getParam('status', 1);
        if (!$id) {
            return '请选择要更改的内容';
        }

        $focus_info = _uri('express_relation', $id);
        if ($status == $focus_info['status']) {
            return '已经执行过此操作，不用重复提交';
        }

        if (1 == $status) {
            _model('express_relation')->update($id, array('status'=>1));
        }

        return 'ok';
    }
}

?>